package com.serookie.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.serookie.entity.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/3/21 23:31
 */
@Component
public class LogoutHandlerConfig implements LogoutSuccessHandler{

    //退出登录后所访问的类
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        Result ok = Result.ok().message("退出成功");
        PrintWriter writer = response.getWriter();
        writer.write(new ObjectMapper().writeValueAsString(ok));
        writer.flush();
        writer.close();
    }
}
